package org.jeecg.common.oa.utils;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.oa.vo.OaDepartmentVo;
import org.jeecg.common.oa.vo.OaMemberVo;
import org.jeecg.common.oa.vo.OaPositionVo;
import org.jeecg.common.properties.config.OaPropertyConfig;
import org.jeecg.common.util.HttpClientUtil;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@Slf4j
public class OaSyncUtils {

    @Autowired
    private OaPropertyConfig oaPropertyConfigBase;
    @Autowired
    private RedisUtil redisUtilBase;

    //当前工具类
    private static OaPropertyConfig oaPropertyConfig;
    private static RedisUtil redisUtil;

    private final static String TOKEN_KEY = "oa_token_u";

    //解决静态方法中不能直接用oaConfig的问题
    @PostConstruct
    public void init() {
        oaPropertyConfig = this.oaPropertyConfigBase;
        redisUtil = this.redisUtilBase;
    }

    /**
     * 获取token信息
     *
     * @param bool 是否强制重新获取
     * @return
     */
    public static String getToke(Boolean bool) {
        String token = null;
//        Object tokenObj = redisUtil.get(TOKEN_KEY);
//        if (tokenObj != null) {
//            token = tokenObj.toString();
//        }
//        if (bool || StringUtils.isBlank(token)) {
//            try {
//                String resultString = HttpClientUtil.doGet(oaPropertyConfig.getGetToken());
//                if (StringUtils.isNotBlank(resultString)) {
//                    try {
//                        JSONObject json = JSON.parseObject(resultString);
//                        token = json.getString("id");
//                    } catch (Exception e) {
//                        token = resultString;
//    //                    e.printStackTrace();
//                    }
//                    redisUtil.set(TOKEN_KEY, token, 2 * 60 * 60);
//                } else {
//                    throw new JeecgBootException("获取TOKEN失败");
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//                throw new JeecgBootException("获取TOKEN失败");
//            }
//        }
        return token == null ? "" : token;
    }

    /**
     * 获取指定单位的所有部门(不包含停用)
     *
     * @return
     */
    public static List<OaDepartmentVo> getDepartment() {
        List<OaDepartmentVo> departmentVos = OaSyncUtils.getList(oaPropertyConfig.getGetDepartment(), OaDepartmentVo.class);
        return departmentVos;
    }

    /**
     * 获取指定单位的所有部门(不包含停用)
     *
     * @return
     */
    public static List<OaPositionVo> getPostByNO() {
        List<OaPositionVo> departmentVos = OaSyncUtils.getList(oaPropertyConfig.getGetPostByNO(), OaPositionVo.class);
        return departmentVos;
    }

    /**
     * 取得指定单位的所有人员(不包含停用人员)
     *
     * @return
     */
    public static List<OaMemberVo> getMembersByAccountId() {
        List<OaMemberVo> departmentVos = OaSyncUtils.getList(oaPropertyConfig.getGetMembersByAccountId(), OaMemberVo.class);
        return departmentVos;
    }

    /**
     * 取得指定单位的所有人员(包含停用人员)
     *
     * @return
     */
    public static List<OaMemberVo> getAllMembersByAccountId() {
        List<OaMemberVo> departmentVos = OaSyncUtils.getList(oaPropertyConfig.getGetAllMembersByAccountId(), OaMemberVo.class);
        return departmentVos;
    }

    public static <T> List<T> getList(String url, Class<T> clazz) {
        List<T> date = new ArrayList<>();
        String token = OaSyncUtils.getToke(false);
        if (StringUtils.isNotBlank(token)) {
            Map<String, String> param = new HashMap<>();
            param.put("token", token);
            String resultString = HttpClientUtil.doGet(url, param);
            log.debug(url + " OA RESULTSRING", resultString);
            if (StringUtils.isNotBlank(resultString)) {
//                BaseThingsModel<Map<String, Object>> r = JSON.parseObject( new String(response.getBody(), "UTF-8"), new TypeReference<BaseThingsModel<Map<String, Object>>>(){});
                try {
                    date = JSON.parseArray(resultString, clazz);
                } catch (Exception e) {
                    e.printStackTrace();
                    OaSyncUtils.refreshToken();
                    throw new JeecgBootException("获取信息失败");
                }
            } else {
                throw new JeecgBootException("获取信息失败");
            }
        }
        return date;
    }

    private static void refreshToken() {
//        redisUtil.del(TOKEN_KEY);
        OaSyncUtils.getToke(true);
    }


    public static void main(String[] args) {
        //http://101.132.226.143:8081/seeyon/rest/token/rest/50489ed0-8697-4d1f-9a4c-460c3f5507cd
//        System.out.println(getToke("rest", "50489ed0-8697-4d1f-9a4c-460c3f5507cd"));
        System.out.println(getToke(false));
    }

}
